<template>
  <div>
    <a-select
      :allowClear="true"
      :mode="multi==true?'multiple':'default'"
      :showSearch="false"
      style="width: 100%"
      placeholder="请选择人员"
      @deselect="deselect"
      v-model="empId"
      :open="open"
      :disabled="disabled"
      @change="selectChange"
      @dropdownVisibleChange="selectIds">
      <a-select-option v-for="item in selectedObj" :key="item.empId">{{ item.realname }}</a-select-option>
    </a-select>
    <select-user-by-dep-modal
      ref="inputModal"
      :multi="multi"
      :normalData="normalData"
      @choseObj="choseObj" />
  </div>
</template>

<script>
import SelectUserByDepModal from './modules/SelectUserByDepModal'
import { getAction } from '@/api/manage'

export default {
  name: 'SelectUserByDep',
  components: { SelectUserByDepModal },
  props: {
    disabled: {
      type: Boolean,
      required: false,
      default: false
    },
    open: {
      type: Boolean,
      required: false,
      default: false
    },
    value: {
      type: String,
      required: false,
      default: null
    },
    multi: {
      type: Boolean,
      default: false,
      required: false
    },
    props: {
      type: Object,
      default: null
    },
    // 是否只查询正常的数据（在职的员工+实际部门），默认false:虚拟部门+离职员工都查
    normalData:{
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      empId: [],
      selectedObj: [],
      deleteIds: [],
      list: [],
      objs: '',
      url: {
        queryById: '/api/erpmng/sys/user/queryByEmpId',
        queryByIds: '/api/erpmng/sys/user/queryByEmpIds'
      }
    }
  },
  mounted() {
    this.initObj(this.value);
  },
  watch: {
    value(val) {
      this.initObj(val);
    },
    immediate: true
  },
  model: {
    prop: 'value',
    event: 'change'
  },
  methods: {
    initObj(empId) {
      this.deleteIds = [];
      this.selectedObj = [];
      if (empId && empId.length > 0) {
        this.empId = empId.split(',');
        let url = '';
        let param = {};
        if (this.empId.length == 1) {
          url = this.url.queryById;
          param = { empId: empId };
        } else {
          url = this.url.queryByIds;
          param = { empId: empId };
        }
        getAction(url, param).then((res) => {
          if (res.success) {
            if (!Array.isArray(res.result)) {
              if (!res.result) {
                return
              }
              this.selectedObj.push(res.result);
            } else {
              for (let i = 0; i < res.result.length; i++) {
                this.selectedObj.push(res.result[i]);
              }
            }
            this.$nextTick(() => {
              if (this.$refs.inputModal) {
                this.$refs.inputModal.edit(res.result, this.empId);
              }
            });
          }
        });
      } else {
        this.empId = [];
        this.$refs.inputModal.edit(null, null);
      }
    },
    resetObj() {
      this.userType = false;
      this.empId = [];
      this.selectedObj = [];
      this.deleteIds = [];
      this.open = false;
      this.$refs.inputModal.edit(null, null);
    },
    selectChange() {
      if (!this.empId || this.empId.length == 0) {
        this.selectedObj = []
        this.choseObj(this.selectedObj)
      }
    },
    selectIds() {
      if (!this.disabled) {
        this.deleteId();
        this.$refs.inputModal.add(this.selectedObj, this.empId);
      }
    },
    deselect(value, option) {
      this.deleteIds.push(value);
      this.deleteId();
      this.choseObj(this.selectedObj);
    },
    deleteId() {
      let that = this;
      this.selectedObj = this.selectedObj.filter(function (e) {
        return that.deleteIds.indexOf(e.empId) == -1;
      })
      this.deleteIds = [];
    },
    // 子modal回调
    choseObj: function (list) {
      this.selectedObj = list;
      this.empId = [];
      for (var i = 0; i < list.length; i++) {
        this.empId.push(list[i].empId);
      }
      if (this.empId && this.empId.length > 0) {
        this.objs = this.empId.join(',');
      } else {
        this.objs = ''
      }
      this.$emit('change', this.objs);
      this.$emit('back', this.selectedObj);
    }
  }
}
</script>

<style scoped>

</style>
